<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 11:27
 */

namespace app\admin\controller;


use app\admin\model\Admin;
use think\Session;

class LoginController extends BaseController
{
    public function login()
    {
        if(Session::get('admin')){
            $this->success('您已经登录过了',"admin/index/index",'',1);
            die();
        }
        if($this->request->isPost()){
            $logData = input('post.');
            $result = $this->validate($logData,'Login');
            if(true !== $result){
                // 验证失败 输出错误信息
                $this->error($result,'login');
                die();
            }

            $password = MD5($logData['password']);
            $user_name = $logData['user_name'];
            $result = Admin::get(['user_name' => $user_name,'password' => $password]);
            if(!$result){
                $this->error('用户名或密码不正确','login');
                die();
            }
            Session::set('admin',$result['id']);
            Session::set('admin_name',$result['user_name']);
            $this->success('您已成功登录','admin/index/index','',1);
        }
        return $this->fetch();
    }

    public function logOut()
    {
        Admin::where('id',Session::get('admin'))->update(['last_login' => date('Y-m-d H:i:s',time()),'last_ip'=>get_server_ip()]);
        Session::delete('admin');
        $this->success('您已经安全退出','login','',1);
    }

}